
var option = [
		{ 
		  title:{
			 text: '查询高频词',
			 subtext: '前10高频词查询次数',
			 left: 'center' 
		  } ,
		  tooltip: {
			trigger: 'axis',
			axisPointer: {
			  type: 'shadow'
			}
		  },
		  grid: {
			left: '3%',
			right: '4%',
			bottom: '3%',
			containLabel: true
		  },
		  xAxis: 
			{
			  type: 'category',
			  data:[] ,
			  axisTick: {
				alignWithLabel: true
			  },
			   axisLabel: {
				   width:40,
				   interval: 0  , 
				   overflow:"break"
				 },
			}
		  ,
		  yAxis: [
			{
			  type: 'value'
			}
		  ],
		  series: [
			{
			  name: '次数',
			  type: 'bar',
			  barWidth: '60%',
			  data: []
			}
		  ]
	}, {
		 title:{
			 text: '时间段查询数',
			 subtext: '0点-9点',
			 left: 'center' 
		 } ,
		 tooltip: {
		    trigger: 'axis'
		  },
		  xAxis: {
		    type: 'category',
		    data: [],
			axisLabel: { 
			         width:30,
			         interval: 0  , 
			         overflow:"break"
			       },
		  },
		  yAxis: {
		    type: 'value'
		  },
		  series: [
		    {
		      data: [],
		      type: 'line'
		    }
		  ]

	}, {
		
		  title: {
		    text: '查询类型占比',
		    subtext: 'URL占比',
		    left: 'center'
		  },
		  tooltip: {
		    trigger: 'item',
			formatter: '{a} <br/>{b} : {c} ({d}%)'
			
		  },
		  legend: {
		    orient: 'vertical',
		    left: 'left'
		  },
		  series: [
		    {
		      name: '点击数',
		      type: 'pie',
		      radius: '50%',
		      data: [],
		      emphasis: {
		        itemStyle: {
		          shadowBlur: 10,
		          shadowOffsetX: 0,
		          shadowColor: 'rgba(0, 0, 0, 0.5)',
				  //pointFormat: '{series.name}: {point.y} {point.percentage:.1f}%'
		        }
		      }
		    }
		  ]

	}, {
		  title:{
			text: '选择占比',
			subtext: '用户选择',
			left: 'center'  
		  } ,
		  tooltip: {
		    trigger: 'item',
			formatter: '{a} <br/>{b} : {c} ({d}%)'
			
		  },
		  legend: {
			orient: 'vertical',
		    bottom:"bottom"
		  },
		  series: [
		    {
		      name: '点击数',
		      type: 'pie',
		      radius: ['40%', '70%'],
		      avoidLabelOverlap: false,
		      itemStyle: {
		        borderRadius: 10,
		        borderColor: '#fff',
		        borderWidth: 2
		      },
		      label: {
		        show: false,
		        position: 'center',
				pointFormat: '{series.name}: {point.y} {point.percentage:.1f}%'
		      },
		      emphasis: {
		        label: {
		          show: true,
		          fontSize: '40',
		          fontWeight: 'bold',
				  pointFormat: '{series.name}: {point.y} {point.percentage:.1f}%'
		        }
		      },
		      labelLine: {
		        show: false
		      },
		      data: []
		    }
		  ]

	}]


var sqllist = [
	"select count(*) nums from midresult" ,
	"select distinct(keyword), resultcount from midresult  order by resultcount desc limit 10",
	"select count(distinct(keyword)) nums from midresult ",
	
	"select count(distinct(uid))  nums from midresult ",
	"select distinct(url)  , keyword , userselect from midresult where  userselect>0 order by userselect limit 10",
	"select count(keyword) nums , keyword from midresult group by keyword order by nums desc limit 10" ,
	
	"select count(*) peoplecount from ( select count(uid) nums, uid from midresult group by uid having nums>2) w" ,
	"select count(*) clickcount from (select userselect from midresult where userselect=1) w",
	"select count(*) urlcount from midresult where keyword like '%www.%.%'" ,
	
	"select uid , count(uid) num from midresult where keyword like '%淘宝%' group by uid order by num desc limit 10",
	"select count(stime) num , stime  from (select substr( searchtime , 0 , 5 ) stime  from midresult ) w group by stime order by num desc " ,
	"select keyword , count(keyword) nums from midresult where keyword like '%淘宝%' group by keyword order by nums desc"	
]

var visualization = [
	"select count(keyword) series , keyword xAxis from midresult group by keyword order by series desc limit 10",
	"select count(stime) series , stime xAxis  from (select substr( searchtime , 0 , 5 ) stime  from midresult ) w group by stime ",
	"select count(*) value, 'sum' name from midresult  union select count(*) value, 'url' name  from midresult where keyword like '%.%'" ,
	"select count(*) value , '1-5' name from midresult where userselect>=1 and userselect<=5 union "+
	"select count(*) value , '>=5' name from midresult where userselect > 5 and userselect<=10 union " +
	"select count(*) value , '>=10' name from midresult where userselect > 10 "
	
	
]


function loadData(sql , loadstatus){
	
	fetch("http://localhost/map?sql="+encodeURIComponent(sql))
	.then(data=>data.json())
	.then(result=>{
		loadstatus.fullscreenLoading = false
		document.getElementById("mytable").innerHTML="" ;
		var tablecontent = "<tr>" ;
		var tablehead = Object.keys(result[0]);
		tablehead.forEach(val =>{
			tablecontent+="<th>"+val+"</th>" ;
		})
		tablecontent+="</tr>"
		for(let i = 0 ; i < result.length ; i++) {
			tablecontent+="<tr>"
			Object.keys(result[i]).forEach(val=>{tablecontent+="<td>"+result[i][val]+"</td>" ;})
			tablecontent+="</tr>"
		}
		document.getElementById("mytable").innerHTML=tablecontent;
	})
}

function drawing(elId , loadstatus) {
	//document.getElementById("draw").innerHTML=""
	var myChart = echarts.init(document.getElementById("draw"));
	myChart.clear();
	let index = parseInt(elId)
	fetch("http://localhost/map?sql="+encodeURIComponent(visualization[index-1]))
	.then(data=>data.json())
	.then(result => {
		loadstatus.fullscreenLoading = false
		myChart.setOption(optionFactroy(index-1 , result));;
	})
}

function optionFactroy(initindex , result) {
	if(initindex < 2) {
		var xdata = []
		var ydata = []
		for(let i = 0 ; i < result.length ; i++) {
			xdata.push(result[i].xaxis)
			ydata.push(result[i].series)
		}
		option[initindex].xAxis.data = xdata ;
		option[initindex].series[0].data = ydata ;
	} else {
		var sdata = []
		let tablename = Object.keys(result[0])[1].split(".")[0]
		for(let i = 0 ; i < result.length ; i++) {
			sdata.push({value:result[i][tablename+".value"] , name:result[i][tablename+".name"]})
			
		}
		option[initindex].series[0].data = sdata ;
	}
	
	return option[initindex]
}